package org.example.shop.service.impl;

import org.example.shop.config.RabbitMQConfig;
import org.example.shop.service.MailService;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class MailServiceImpl implements MailService {
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.from}")
    private String from;
    @Value("${server.port}")
    private int port;

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public boolean sendVerifyCode(String to, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//谁发的
        message.setTo(to); //谁要接收
        message.setSubject("验证码");//邮件标题
        message.setText("当前的验证码为:" + code);//邮件内容
        try {
            // 发送邮件
            javaMailSender.send(message);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean sendVerifyEmail(String to, String token) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);//谁发的
        message.setTo(to); //谁要接收
        message.setSubject("验证邮箱 - 如果不是你的操作请忽略");//邮件标题
        message.setText("请点击下面的链接验证您的邮箱\n" +
                "http://localhost:" + port + "/user/verify?token=" + token);//邮件内容
        try {
            // 发送邮件
            javaMailSender.send(message);
            return true;
        } catch (MailException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void sendVerifyEmailMessage(String to, String token) {
        // 发送的数据
        Map<String, String> data = new HashMap<>();
        data.put("email", to);
        data.put("token", token);
        int delay = 10000;
        // 发送
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.RABBITMQ_DEFAULT_DIRECT_EXCHANGE, // 消息发送给谁
                RabbitMQConfig.RABBITMQ_DEFAULT_DIRECT_ROUTING,  // 消息通过哪个路由发送
                data);
    }

    private MessagePostProcessor getDelay(int delay) {
        return new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //给消息设置延迟毫秒值
                message.getMessageProperties().setExpiration(String.valueOf(delay));
                return message;
            }
        };
    }
}
